{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "continuous-captain",
   "metadata": {},
   "outputs": [],
   "source": [
    "cd .."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "closing-bishop",
   "metadata": {},
   "outputs": [],
   "source": [
    "import dlib\n",
    "import glob\n",
    "import os\n",
    "from tqdm import tqdm\n",
    "from utils.alignment import align_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "false-healthcare",
   "metadata": {},
   "outputs": [],
   "source": [
    "images_path = '/disk2/danielroich/Sandbox/Data/Images/barcelona'\n",
    "SHAPE_PREDICTOR_PATH = 'pretrained_models/shape_predictor_68_face_landmarks.dat'\n",
    "IMAGE_SIZE = 1024"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "coordinate-australia",
   "metadata": {},
   "outputs": [],
   "source": [
    "predictor = dlib.shape_predictor(SHAPE_PREDICTOR_PATH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accurate-allowance",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.chdir(images_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "australian-yellow",
   "metadata": {},
   "outputs": [],
   "source": [
    "images_names = glob.glob(f'*')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "secure-concentrate",
   "metadata": {},
   "outputs": [],
   "source": [
    "images_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "basic-pickup",
   "metadata": {},
   "outputs": [],
   "source": [
    "aligned_images = []\n",
    "for image_name in tqdm(images_names):\n",
    "    try:\n",
    "        aligned_image = align_face(filepath=f'{images_path}/{image_name}',\n",
    "                                       predictor=predictor, output_size=IMAGE_SIZE)\n",
    "        aligned_images.append(aligned_image)\n",
    "    except Exception as e:\n",
    "        print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "textile-extraction",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.makedirs(f'{images_path}/aligned', exist_ok=True)\n",
    "os.makedirs(f'{images_path}/aligned/0', exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "civic-dictionary",
   "metadata": {},
   "outputs": [],
   "source": [
    "for image, name in zip(aligned_images,images_names):\n",
    "    real_name = name.split('.')[0]\n",
    "    try:\n",
    "        image.save(f'{images_path}/aligned/0/{real_name}.jpeg')\n",
    "    except Exception as e:\n",
    "        print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "tough-celebrity",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
